import Vue from 'vue'
import App from './App.vue'
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
import './assets/global.css'
import 'bootstrap/dist/css/bootstrap.css'
import '@fortawesome/fontawesome-free/css/all.css'
import axios from "axios";
import VueRouter from 'vue-router';
import 'vue-cropperjs/dist/VueCropper'
import router from './router/IndexPage';
import store from './store/index';

axios.defaults.baseURL = 'http://localhost:8090';
axios.defaults.withCredentials = true;
// 创建Axios实例
const http = axios.create({
    baseURL: 'http://localhost:8090',
    withCredentials: true
});
const axiosInstance = axios.create({
    baseURL: process.env.VUE_APP_API_URL,
    responseType: 'json' // 默认响应类型为 JSON
});


// 请求拦截器
http.interceptors.request.use(config => {
    const token = store.state.token;
    if (token) {
        config.headers.Authorization = `Bearer ${token}`;
    }
    return config;
});

// 响应拦截器
http.interceptors.response.use(
    response => response,
    error => {
        if (error.response?.status === 401) {
            store.dispatch('logout');
            router.push(`/LoginPage?redirect=${encodeURIComponent(router.currentRoute.fullPath)}`);
        }
        return Promise.reject(error);
    }
);

Vue.prototype.$axios = http;
Vue.prototype.$httpUrl = 'http://localhost:8090';

Vue.config.productionTip = false;
Vue.use(VueRouter);
Vue.use(ElementUI, { size: 'small' });

new Vue({
    router,
    store,
    render: h => h(App),
}).$mount('#app');
